﻿// UART2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <stdio.h>
#include <Windows.h>


int main()
{

	FILE* file0;
	fopen_s(&file0, "Rk.bin", "rb");
	if (file0 == 0)
	{
		printf("文件打开失败");
		return 0;
	}
	HANDLE hCom;
	hCom = CreateFile(TEXT("COM3"),//COM3口
		GENERIC_READ | GENERIC_WRITE,//允许读和写
		0,//独占方式
		NULL,
		OPEN_EXISTING,//打开而不是创建
		0,//同步方式
		//FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERAPPLE,//重叠方式
		NULL);
	if (hCom == (HANDLE)-1)
	{
		printf("打开COM失败！");
		return 0;
	}
	COMMTIMEOUTS CTOs;
	CTOs.ReadIntervalTimeout = 1000;
	CTOs.ReadTotalTimeoutMultiplier = 500;
	CTOs.ReadTotalTimeoutConstant = 5000;
	CTOs.WriteTotalTimeoutMultiplier = 1000;
	CTOs.WriteTotalTimeoutConstant = 1000;
	SetCommTimeouts(hCom, &CTOs);
	DCB dcb;
	GetCommState(hCom, &dcb);
	dcb.BaudRate = 115200;//波特率为115200
	dcb.ByteSize = 8;//每个字节有8位
	dcb.Parity = EVENPARITY;//偶数奇偶校验位
	dcb.StopBits = ONESTOPBIT;//1个停止位
	dcb.fParity = TRUE;
	dcb.fNull = FALSE;
	SetCommState(hCom, &dcb);
	PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);//在读写串口之前清空缓冲区

	int flag0 = 0, height = 0, width = 0, preb = 0;
	int bh = 0, bl = 0, totaln = 0, npoint = 0, len = 0;
	char sendbuf[8], ope[8],recvbuf[32];
	printf("高:%d\r\n",height);
	scanf_s("%d", &height);//输入高
	printf("宽:%d\r\n",width);
	scanf_s("%d", &width);//输入宽
	len = height * width / 100;
	printf("准备完成");
	scanf_s("%d", &flag0);
	if (flag0)
	{
		if (!WriteFile(hCom, "cmd4", 4, NULL, NULL))
		{
			printf("开头发送失败\n");
		}
		DWORD readnum = 0;
		memset(recvbuf, '\0', 32);
		ReadFile(hCom, recvbuf, 10, &readnum, NULL);
		printf("%s\r\n", recvbuf);
		sprintf_s(sendbuf, "%d", height);
		Sleep(2);
		if (!WriteFile(hCom, sendbuf, 4, NULL, NULL))
		{
			printf("高发送失败\n");
		}
		sprintf_s(sendbuf, "%d", width);
		if (!WriteFile(hCom, sendbuf, 4, NULL, NULL))
		{
			printf("宽发送失败\n");
		}
		fseek(file0, 0, SEEK_SET);
		Sleep(20);
		while (preb<66)
		{
			if (!WriteFile(hCom, "FFFF", 4, NULL, NULL))
			{
				printf("垫头发送失败\n");
			}
			preb++;
		}
		while (1)
		{
			memset(sendbuf, '\0', 8);
			memset(ope, '\0', 8);
			bh = fgetc(file0);
			if (bh == EOF)
			{
				if (!WriteFile(hCom, "END\0", 4, NULL, NULL))
				{
					printf("结束发送失败\n");
				}
				printf("结束\r\n");
				fclose(file0);
				CloseHandle(hCom);
				return 0;
			}
			bl = fgetc(file0);
			sprintf_s(sendbuf, "%x", bh);
			sprintf_s(ope, "%x", bl);
			strcat_s(sendbuf, ope);
			if (totaln >= 0)
			{
				if (!WriteFile(hCom, sendbuf, 4, NULL, NULL))
				{
					printf("内容发送失败\n");
				}
			}
			totaln++;
			if (npoint != (totaln / len))
			{
				npoint = totaln;
				switch (npoint)
				{
				case 10:
					printf("10%%--OK\r\n");
					break;
				case 20:
					printf("20%%--OK\r\n");
					break;
				case 30:
					printf("30%%--OK\r\n");
					break;
				case 40:
					printf("40%%--OK\r\n");
					break;
				case 50:
					printf("50%%--OK\r\n");
					break;
				case 60:
					printf("60%%--OK\r\n");
					break;
				case 70:
					printf("70%%--OK\r\n");
					break;
				case 80:
					printf("80%%--OK\r\n");
					break;
				case 90:
					printf("90%%--OK\r\n");
					break;
				default:
					break;
				}
			}
		}
	}
	else
	{
		fclose(file0);
		CloseHandle(hCom);
		return 0;
	}

	fclose(file0);
	CloseHandle(hCom);


	return 0;
}
